package components;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import com.bbn.openmap.omGraphics.OMPoint;

public class NodeList {

	private int count;
	private ArrayList<Node> nodes = null;;
	
	private String filePath;
	
	public NodeList(){
		count = 0;
		nodes = new ArrayList<Node>();
		filePath = "E:/projects/java/INZYNIERKA/roads-editor/src/resources/nodes.txt";
		
		loadNodesToList();
	}
	
	public NodeList(String filePath){
		count = 0;
		nodes = new ArrayList<Node>();
		this.filePath = filePath;
		
		loadNodesToList();
	}
	
	public boolean loadNodesToList(){
		File file = new File(filePath);
		BufferedReader reader = null;
		
		try{
			reader = new BufferedReader(new FileReader(file));
			String line = null;
			
			if((line = reader.readLine()) != null){
				addCount(line);
			}
			int l = 1;
			while((line = reader.readLine()) != null){
				nodes.add( addNode(line, l) );
			}
		}catch(FileNotFoundException e){
			System.out.println("Nie odnaleziono pliku ["+ filePath +"]");
		}catch(IOException e){
			System.out.println("IO exception\n"+e);
		}
		finally{
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
		
		return false;
	}
	
	private Node addNode(String line, int l) {
		String[] split = line.split(" ");
		int[] nodeVals = new int[4];
		
		for(int i=0,j=0; i<split.length; i++){
			if(!split[i].equals("")){
				nodeVals[j] = Integer.parseInt(split[i].trim());
				j++;
			}
		}
//		System.out.println(nodeVals[0]+" "+nodeVals[1]+" "+ nodeVals[2]+" "+ nodeVals[3]);
		return new Node(nodeVals[0], nodeVals[1], nodeVals[2], nodeVals[3],l);
	}

	private void addCount(String line) {
		count = Integer.parseInt(line.trim());
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public ArrayList<Node> getNodes() {
		return nodes;
	}

	public void setNodes(ArrayList<Node> nodes) {
		this.nodes = nodes;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public int findNode(float lat, float lon){
		int i;
		boolean found = false;
		
		for(i=0;i<this.nodes.size();i++){
			if(this.nodes.get(i).getLattitude() == lat){
				if(this.nodes.get(i).getLongtitude() == lon){
					found = true;
					break;
				}
			}
		}
		if(found)
			return i;
		else
			return -1;
	}
//	public static void main(String args[]){
//		NodeList n = new NodeList();
//		System.out.println(n.getCount());
//		System.out.println(n.getNodes().get(0).getLattitude()+" "+n.getNodes().get(0).getLongtitude());
//		System.out.println(n.getNodes().get(1).getLattitude()+" "+n.getNodes().get(1).getLongtitude());
//		System.out.println(n.getNodes().get(3).getLattitude()+" "+n.getNodes().get(3).getLongtitude());
//		System.out.println(n.getNodes().get(4).getLattitude()+" "+n.getNodes().get(4).getLongtitude());
//		
//		
//	}

	public Node getNodeById(int i) {
//		System.out.println(i);
		return nodes.get(i-1);
	}

	public int findGraphicPosition(OMPoint object) {
		int pos = -1;
		
		float lat = object.getLat();
		float lon = object.getLon();
		
		for(int i=0; i<nodes.size(); i++){
			if(lat == nodes.get(i).getLattitude() && lon == nodes.get(i).getLongtitude()){
				pos = i;
				break;
			}
		}
		
		return pos;
	}

	public void changeLine(int pos) {
		for(int i=pos; i<nodes.size(); i++){
			nodes.get(i).decrementLine();
		}
	}
	
}
